package model.admin;

import ecom.beans.AccountBean;
import ecom.beans.EcomAdminRemote;
import ecom.shell.ShellRemote;
import model.AbstractPage;

/**
 *
 * @author Sebastien
 */
public class AccountManagement extends AbstractPage {

    private AccountBean account;
    private static final String xsl = "C://JAVA//wkspace//ecom//ecom-war//web//XSL/admin/account.xsl";
    private static final String formPage = "admin/account/form.jsp";
    private static final String listPage = "admin/list.jsp";
    
    public AccountBean getAccount() {
        return account;
    }

    public void setAccount(AccountBean account) {
        this.account = account;
    }

    public String addOrUpdate() throws Exception {
        String[] id = (String[]) getParameters().get("id");
        String[] requestAction = (String[]) getParameters().get("action");
        EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");

        if ((id != null && !"0".equalsIgnoreCase(id[0])) || (account != null && account.getAccountId() != 0)) {

            // update
            if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
                admin.updateAccount(account.getAccountId(), account.getAccountOwner(),
                        account.getBalance(), account.getLogin(),
                        account.getPass(), account.getSurname(),
                        account.getAddress(), account.getBirthdate());
            } // affichage du formulaire pré-rempli
            else {
                account = admin.getAccountById(Integer.valueOf(id[0]));
                this.setPage(formPage);
                return SUCCESS;
            }
        } else {

            // insert
            if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
                admin.createAccount(account.getAccountOwner(), account.getBalance(), account.getLogin(),
                        account.getPass(), account.getSurname(), account.getAddress(), account.getBirthdate());
            } // affichage du formulaire pré-rempli
            else {
                this.setPage(formPage);
                return SUCCESS;
            }
        }
        return list();
    }

    public String list() throws Exception {
        ShellRemote shell = (ShellRemote) getSession().get("shell");
        this.setDataToPrint(shell.executeCommand("account"));
        this.setXslPath(xsl);
        this.setPage(listPage);
        return SUCCESS;
    }

    public String delete() throws Exception {
        String[] id = (String[]) getParameters().get("id");

        if (id != null && !"".equalsIgnoreCase(id[0])) {
            EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");
            admin.removeAccount(Integer.valueOf(id[0]));
        }
        return list();
    }

    @Override
    public void validate() {
        String[] requestAction = (String[]) getParameters().get("action");
        if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
            this.setPage(formPage);
  
            if (account.getAccountOwner().length() == 0) {
                this.addFieldError("account.accountOwner", "Préciser le nom");
            }
            if (account.getSurname().length() == 0) {
                this.addFieldError("account.surname", "Préciser le prénom");
            }
            if (account.getAddress().length() == 0) {
                this.addFieldError("account.address", "Préciser l'addresse");
            }
            if (account.getLogin().length() == 0) {
                this.addFieldError("account.login", "Préciser le login");
            }
            if (account.getPass().length() == 0) {
                this.addFieldError("account.pass", "Préciser le pass");
            }
        }
    }

    @Override
    public String execute() throws Exception {
        message = "AccountManagement()";
        return SUCCESS;
    }
}